package cn.pfund.controller.shop;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.pfund.controller.BaseController;
import cn.pfund.entity.Product;
import cn.pfund.entity.ProductType;
import cn.pfund.service.ProductService;
import cn.pfund.service.ProductTypeService;
import cn.pfund.service.TagService;
import cn.pfund.util.ParamBean;

/**
 * 商品
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2017年1月20日
 */
@Controller("shopProductController")
@RequestMapping("/shop/product")
public class ProductController extends BaseController {
    @Resource(name="productService")
    private ProductService productService;
    @Resource(name="productTypeService")
    private ProductTypeService productTypeService;
    @Resource(name="tagService")
    private TagService tagService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
//    	paramBean.setUrl(getBasePath(request) + "shop/product/list.shtml");
//		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
//		paramBean.setPage(p);
//		Pagination<Product> pagination = productService.getPagination(paramBean);
//		request.setAttribute("pagination", pagination);
//		request.setAttribute("paramBean", paramBean);
    	ProductType productType = null;
    	if (paramBean.getProductTypeId() != null && paramBean.getProductTypeId().intValue() > 0) {
			productType = productTypeService.getById(ProductType.class, paramBean.getProductTypeId());
		}
		List<Product> products = productService.getAllProduct(paramBean);
		request.setAttribute("products", products);
		request.setAttribute("productType", productType);
		request.setAttribute("tags", tagService.getAllTag());
		return "shop/product/list";
	}
	
	@RequestMapping(value = "/detail",method=RequestMethod.GET)
	public String detail(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		if (paramBean == null || paramBean.getProductId() == null || paramBean.getProductId().intValue() < 1) {
			return ERROR_VIEW;
		}
		Product product = productService.getById(Product.class, paramBean.getProductId());
		if (product == null) {
			return ERROR_VIEW;
		}
		List<Product> products = productService.getAllProduct(paramBean);
		request.setAttribute("product", product);
		request.setAttribute("products", products);
		return "shop/product/detail";
	}
}